package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.pocketsweet.C;
import com.pocketsweet.R;
import com.pocketsweet.chatlib.controller.MolianContactManager;
import com.pocketsweet.model.MLFriendApply;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.NewLover;
import com.pocketsweet.ui.Profile;
import com.pocketsweet.utils.StringUtils;

/**
 * 
 * @author Kam
 * 
 */
public class NewLoverListAdapter extends BaseListAdapter<MLFriendApply> {
	private Context context;

	public NewLoverListAdapter(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	public NewLoverListAdapter(Context ctx, List<MLFriendApply> datas) {
		super(ctx, datas);
		this.context = ctx;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_new_lover, parent,
					false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	public class ViewHolder implements View.OnClickListener {
		ImageView imgAvatar;
		ImageView tvHasMoney;

		TextView tvuserName;
		TextView tvUserAge;
		TextView tvFrom;
		TextView tvType;
		TextView tvStar;
		TextView tvWilling;
		TextView tvAccepted;
		TextView tvRemark;
		// TextView tvNoRemark;
		MLFriendApply item;
		MLUser mlUser;
		int likeCount = 0;
		public View parent;

		public ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
			tvHasMoney = (ImageView) parent.findViewById(R.id.tvHasMoney);

			tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
			tvUserAge = (TextView) parent.findViewById(R.id.tvUserAge);
			tvFrom = (TextView) parent.findViewById(R.id.tvFrom);
			tvStar = (TextView) parent.findViewById(R.id.tvStar);
			tvWilling = (TextView) parent.findViewById(R.id.tvWilling);
			tvAccepted = (TextView) parent.findViewById(R.id.tvAccepted);
			tvWilling = (TextView) parent.findViewById(R.id.tvWilling);
			tvRemark = (TextView) parent.findViewById(R.id.tvRemark);
			tvType = (TextView) parent.findViewById(R.id.tvType);
			// tvNoRemark = (TextView) parent.findViewById(R.id.tvNoRemark);
		}

		public void refreshView(MLFriendApply item, int i) {
			this.item = item;
			// 设置图片
			mlUser = item.getApplier();
			String avatar = mlUser.getAvatarUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displaySquare(avatar, imgAvatar);
			} else {
				imgAvatar.setImageResource(R.drawable.img_avatar_girl);
			}
			if (!StringUtils.isEmpty((mlUser.getNickname()))) {
				tvuserName.setText(mlUser.getNickname());
			}
			if (!StringUtils.isEmpty((mlUser.getBelong()))) {
				tvType.setText(mlUser.getBelong());
			}
			// 设置年龄
			if (!StringUtils.isEmpty(String.valueOf(mlUser.getAgeNum()))) {
				tvUserAge.setText(String.valueOf(mlUser.getAgeNum()));
			}
			// 设置性别颜色
			if (mlUser.getGender() == 1) {
				tvUserAge
						.setBackgroundResource(R.drawable.shape_rectangle_bule);
			} else if (mlUser.getGender() == 2) {
				// 设置颜色
				tvUserAge.setBackgroundResource(R.drawable.shape_rectangle_red);
			}
			// 设置星座
			tvStar.setText(mlUser.getConstellation());
			// 设置地理位置
			String location = mlUser.getLocationCity();
			if (StringUtils.isEmpty(location)) {
				location = mlUser.getLocationProvince();
			}
			tvFrom.setText(location);
			// 设置描述
			if (!StringUtils.isEmpty(item.getRemark())) {
				tvRemark.setText("留言:" + item.getRemark());
				// tvRemark.setVisibility(View.VISIBLE);
				// tvNoRemark.setVisibility(View.GONE);
			} else {
				tvRemark.setText("Hi~");
				// tvRemark.setVisibility(View.GONE);
				// tvNoRemark.setVisibility(View.VISIBLE);
			}
			if (item.getStatus() == C.FRIEND_APPLYS_SUCCEED) {
				tvWilling.setVisibility(View.GONE);
				tvAccepted.setVisibility(View.VISIBLE);

			} else {
				tvWilling.setVisibility(View.VISIBLE);
				tvAccepted.setVisibility(View.GONE);
			}
			if (mlUser.getType() == C.USER_TYPE_NORMAL) { // 设置是否编辑推荐
				tvHasMoney.setVisibility(View.GONE);
			} else {
				tvHasMoney.setVisibility(View.VISIBLE);

			}

			tvWilling.setOnClickListener(this);
			parent.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			if (v.equals(tvWilling)) {
				if (((TextView) v).getText().equals("我愿意")) {
					AVAnalytics.onEvent(ctx, "我愿意（新的恋人）");
					NewLover.loading.show();
					MolianContactManager.getInstance().agreeAddContact(
							item.getApplier(), item.getObjectId(), v);
				}
			} else if (v.equals(parent)) {
				Intent intent = new Intent(ctx, Profile.class);
				// Log.e("String", selectedUserId + "  " + likeCount);
				intent.putExtra("userId", mlUser.getObjectId());
				ctx.startActivity(intent);
			}
		}
	}

}
